// pages/information/index/index.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        swiperImg: [],
        informationList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getSwiperImg()
        this.getInformationList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            swiperImg: [],
            informationList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 1000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getInformationList()
    },

    onShareAppMessage: function(res) {
        let userId = wx.getStorageSync('userId')
        let storeId = wx.getStorageSync('storeId')
        if (userId) {
            return {
                title: '资讯',
                path: '/pages/information/index/index?userShare=' + userId
            }
        }
    },

    getSwiperImg: function() {
        const that = this
        let data = {
            status: 1
        }
        common.post('Information/Information_slide', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    swiperImg: res
                })
            }
        }).catch(function(e) {})
    },

    getInformationList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let data = {
            status: 1,
            limit: 10,
            page: this.data.nowPage
        }
        let informationList = this.data.informationList
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中'
        })
        common.post('Information/Information_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    informationList: informationList,
                    isAll: true
                })
            } else {
                informationList = informationList.concat(res)
                that.setData({
                    informationList: informationList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    goLink: function(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.link
        })
    },

    goDetail: function(e) {
        wx.navigateTo({
            url: '/pages/information/detail/detail?id=' + e.currentTarget.dataset.id
        })
    }
})